package com.gxa.controller.asset;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.entity.asset.MaintenanceTicket;
import com.gxa.entity.asset.search.MaintenTicketSearchMap;
import com.gxa.service.asset.MaintenanceTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:37:51
 */
@Api(tags = "设备运维--->运维工单")
@RestController
@RequestMapping("/maintenance-ticket")
public class MaintenanceTicketController {
    @Autowired
    private MaintenanceTicketService maintenanceTicketService;


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("运维工单列表接口")
    @GetMapping("/list")
    public R queryAll(MaintenTicketSearchMap map){
        PageInfo<MaintenanceTicket> page = maintenanceTicketService.queryByPage(map);
        return ok("successs").put("data",page);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增运维工单接口")
    @PostMapping("/add")
    public R add(@RequestBody MaintenanceTicket device){
        boolean save = maintenanceTicketService.save(device);
        if (save){
            return ok();
        }
        return error();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除运维工单接口")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id){
        boolean remove = maintenanceTicketService.removeById(id);
        if(remove){
            return ok();
        }

        return error();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询运维工单接口")
    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable("id") Integer id){
        MaintenanceTicket ticket = maintenanceTicketService.getById(id);
        if(ticket != null){
            return ok().put("data",ticket);
        }
        return error("query error");
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑运维工单接口")
    @PutMapping("/edit")
    public R edit(@RequestBody MaintenanceTicket device){
        boolean result = maintenanceTicketService.updateById(device);
        if(result){
            return ok();
        }
        return error("edit error");
    }
}
